package com.hitqz.robot.daemon.quartz.schedule;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hitqz.robot.api.business.entity.RobotWarningRecordEntity;
import com.hitqz.robot.biz.service.RobotWarningRecordService;
import com.hitqz.robot.daemon.quartz.constants.RobotQuartzEnum;
import com.hitqz.robot.daemon.quartz.service.TriColourLightService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * @author xupkun
 * @date 2025/5/7
 */

@Component("triColourLightSchedule")
@RequiredArgsConstructor
@Slf4j
public class TriColourLightSchedule {

    private final TriColourLightService triColourLightService;

    private final RobotWarningRecordService robotWarningRecordService;

    /**
     *  0- 绿灯
     *  1- 橙色
     *  2- 红色
     */
    private Integer lastTurnStatus = -1;

    /**
     * 三色等控制
     * @return
     */
    public String execute() {
        RobotWarningRecordEntity robotWarningRecordEntity = robotWarningRecordService
                .getOne(Wrappers.<RobotWarningRecordEntity>lambdaQuery()
                        .eq(RobotWarningRecordEntity::getFlag, "0").last(" limit 1")
                );
        if (robotWarningRecordEntity == null) {
            if (lastTurnStatus != 0) {
                lastTurnStatus= 0;
                log.info("没有报警，进行初始化");
                triColourLightService.greenLightOn();
                triColourLightService.redLightOff();
                triColourLightService.buzzerOff();
                triColourLightService.yellowLightOff();
            }
        } else {
            // 1-严重 2-预警 3-一般
            if (robotWarningRecordEntity.getLevel() == 1 && lastTurnStatus != 2) {
                lastTurnStatus = 2;
                triColourLightService.greenLightOff();
                triColourLightService.redLightOn();
                triColourLightService.buzzerOn();
                triColourLightService.yellowLightOff();
            } else if ((robotWarningRecordEntity.getLevel() == 2 || robotWarningRecordEntity.getLevel() == 3)
                && lastTurnStatus != 3
            ) {
                lastTurnStatus = 3;
                triColourLightService.greenLightOff();
                triColourLightService.redLightOff();
                triColourLightService.buzzerOff();
                triColourLightService.yellowLightOn();
            }
        }

        return RobotQuartzEnum.JOB_LOG_STATUS_SUCCESS.getType();
    }


}
